/**
 * 
 */
package be.destin.memo;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Christophe
 *
 * This shared class stores in memory, strings for the different keys
 */
public class UserSessionData {

	static private Map <String,Properties> userSessionData = Collections.synchronizedMap(new HashMap<String,Properties>());

	/**
	 * @param request: current request from current user
	 * @param key: application.key of the shared data element
	 * @param value: value to set for that key
	 */
	public static void set (HttpServletRequest request, String key, String value) {
		String userID = request.getRemoteUser();
		synchronized (userSessionData) {
			Properties valuesList = userSessionData.get(userID);
			if (valuesList == null) {
				valuesList = new Properties();
				userSessionData.put(userID, valuesList);
			}			
			valuesList.setProperty(key,value);
		}
	}

	/**
	 * @param request: current request from current user
	 * @param key: application.key of the shared data element
	 * @return shared data string for this key
	 */
	public static String get (HttpServletRequest request, String key) {
		String userID = request.getRemoteUser();
		synchronized (userSessionData) {
			Properties valuesList = userSessionData.get(userID);
			if (valuesList == null) {
				return null;
			}			
			return valuesList.getProperty(key);
		}
	}
}
